package com.ynr.voucher.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ynr.voucher.dao.IDepreciationDao;
import com.ynr.voucher.dao.IVoucherDao;
import com.ynr.voucher.model.FaDepreciationModel;
import com.ynr.voucher.model.FaModel;
import com.ynr.voucher.model.InvoiceItemModel;
import com.ynr.voucher.model.InvoiceModel;
import com.ynr.voucher.model.VoucherModel;
import com.ynr.voucher.service.IFADepreciationService;
import com.ynr.voucher.service.ISubjectService;

@Service
public class FADepreciationServiceImpl implements IFADepreciationService {
	
	private static final Logger logger = LoggerFactory.getLogger(FADepreciationServiceImpl.class);
	
	@Autowired
	private IDepreciationDao depreciationDao;
	
	@Autowired
	private IVoucherDao voucherDao;
	
	@Autowired
	private ISubjectService subjectService;

	@Override
	public void generateDepreciation(InvoiceModel invoice, InvoiceItemModel item, Map<String, Object> companyMap, String fplx, FaModel faModel, String billDate) {
		int companyType = (int)companyMap.get("type");
		long companyId = (long)companyMap.get("id");
		List<FaDepreciationModel> depList = new ArrayList<>();
		double sum = 0;
		if(companyType == 1 || (companyType ==2 && fplx.equals("04"))) {
			sum = (Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum()));
		} else if(companyType ==2 && fplx.equals("01")) {
			sum = Double.valueOf(item.getPrice_sum());
		}
		for(int i=0; i<=faModel.getMonths(); i++) {
			FaDepreciationModel dep = new FaDepreciationModel();
			dep.setCompany_id(companyId);
			dep.setBill_date(nextMonth(billDate, i));
			//采番
			dep.setOrder(1);
			dep.setDate(billDate);
			dep.setClassification(faModel.getType());
			dep.setName(item.getName());
			String originalValue = String.valueOf(sum);
			dep.setOriginal_value(originalValue);
			dep.setScrap_value("0.00");
			dep.setMonths(faModel.getMonths());
			dep.setRm_value_rate("0%");
			String monthlyDep = monthlyDepreciation((Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum())), faModel.getMonths());
			dep.setMonthly_depreciation(monthlyDep);
			dep.setAccumulated_amortization_month(String.valueOf(faModel.getMonths()));
			dep.setDepreciation_this_month(i==0 ? "0" : monthlyDep);
			dep.setAccumulated_depreciation(accumulatedDepreciation(i, monthlyDep, originalValue));
			dep.setNav(String.valueOf(Double.valueOf(originalValue)-Double.valueOf(accumulatedDepreciation(i, monthlyDep, originalValue))));
			depList.add(dep);
			
			Map<String, Integer> retMap = voucherDao.getLastVoucherNum(nextMonth(billDate, i), "invoice");
			int lastVoucherNum = 0;
			if(retMap != null && !retMap.isEmpty()) {
				lastVoucherNum = retMap.get("lastVoucherNum");
			}
			int voucherNum = lastVoucherNum+1;
			VoucherModel voucher = new VoucherModel();
			voucher.setCompany_id(companyId);
			voucher.setTable_name("invoice");
			voucher.setTable_id(invoice.getId());
			voucher.setType("invoice");
			voucher.setBill_date(nextMonth(billDate, i));
			voucher.setVoucher_type(1);   //1-转字凭证
			voucher.setVoucher_num(voucherNum);
			voucher.setAnnex_mount(1); 
			voucher.setAbstract_str(item.getName());
			voucher.setSubjectList(subjectService.generateFaSubjectList(monthlyDep));
			voucher.setSupplier(invoice.getXfMc());
			voucherDao.addVoucher(voucher);
		}
		depreciationDao.insertList(depList);
		
	}

	private static String nextMonth(String baseMonth, int stage) {
		//201908,32
		int nowMonth = Integer.valueOf(baseMonth.substring(4));
		int year = (nowMonth+stage)/12 + Integer.valueOf(baseMonth.substring(0, 4));
		if((nowMonth+stage)%12 < 10) {
			System.out.println(year + "0" + (nowMonth+stage)%12);
			return year + "0" + (nowMonth+stage)%12;
		} else {
			System.out.println(year + "" + (nowMonth+stage)%12);
			return year + "" + (nowMonth+stage)%12;
		}
	}
	
	private String monthlyDepreciation(double originalValue, int months) {
		DecimalFormat df = new DecimalFormat("#.00");
		return String.valueOf(df.format(originalValue/months));
	}
	
	private String accumulatedDepreciation(int month, String monthlyDep, String originalValue) {
		if((month * Double.valueOf(monthlyDep)) >= Double.valueOf(originalValue)) {
			return originalValue;
		}
		return String.valueOf(month * Double.valueOf(monthlyDep));
	}
	
	public static void main(String[] args) {
		/*DecimalFormat df = new DecimalFormat("#.00");
		double i = 26371.68;
		int months = 36;
		System.out.println(i/months);
		System.out.println(df.format(i/months));*/
		nextMonth("201908", 32);
	}

	@Override
	public void generateDepreciation(Map<String, Object> dataMap, InvoiceItemModel item,
			Map<String, Object> companyMap, FaModel faModel, String billDate) {
		int companyType = (int)companyMap.get("type");
		long companyId = (long)companyMap.get("id");
		String fplx = (String)dataMap.get("fplx");
		List<FaDepreciationModel> depList = new ArrayList<>();
		double sum = 0;
		if(companyType == 1 || (companyType ==2 && fplx.equals("04"))) {
			sum = (Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum()));
		} else if(companyType ==2 && fplx.equals("01")) {
			sum = Double.valueOf(item.getPrice_sum());
		}
		for(int i=0; i<=faModel.getMonths(); i++) {
			FaDepreciationModel dep = new FaDepreciationModel();
			dep.setCompany_id(companyId);
			dep.setBill_date(nextMonth(billDate, i));
			//采番
			dep.setOrder(1);
			dep.setDate(billDate);
			dep.setClassification(faModel.getType());
			dep.setName(item.getName());
			String originalValue = String.valueOf(sum);
			dep.setOriginal_value(originalValue);
			dep.setScrap_value("0.00");
			dep.setMonths(faModel.getMonths());
			dep.setRm_value_rate("0%");
			String monthlyDep = monthlyDepreciation((Double.valueOf(item.getPrice_sum()) + Double.valueOf(item.getTax_sum())), faModel.getMonths());
			dep.setMonthly_depreciation(monthlyDep);
			dep.setAccumulated_amortization_month(String.valueOf(faModel.getMonths()));
			dep.setDepreciation_this_month(i==0 ? "0" : monthlyDep);
			dep.setAccumulated_depreciation(accumulatedDepreciation(i, monthlyDep, originalValue));
			dep.setNav(String.valueOf(Double.valueOf(originalValue)-Double.valueOf(accumulatedDepreciation(i, monthlyDep, originalValue))));
			depList.add(dep);
			
			Map<String, Integer> retMap = voucherDao.getLastVoucherNum(nextMonth(billDate, i), "invoice");
			int lastVoucherNum = 0;
			if(retMap != null && !retMap.isEmpty()) {
				lastVoucherNum = retMap.get("lastVoucherNum");
			}
			int voucherNum = lastVoucherNum+1;
			VoucherModel voucher = new VoucherModel();
			voucher.setCompany_id(companyId);
			voucher.setTable_name("invoice");
			voucher.setTable_id((long)dataMap.get("id"));
			voucher.setType("invoice");
			voucher.setBill_date(nextMonth(billDate, i));
			voucher.setVoucher_type(1);   //1-转字凭证
			voucher.setVoucher_num(voucherNum);
			voucher.setAnnex_mount(1); 
			voucher.setAbstract_str(item.getName());
			voucher.setSubjectList(subjectService.generateFaSubjectList(monthlyDep));
			voucher.setSupplier((String)dataMap.get("xfMc"));
			voucherDao.addVoucher(voucher);
		}
		depreciationDao.insertList(depList);
	}
	
}
